package com.zhihuishu.micro.course.openapi.app.impl.teacher;

import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.zhihuishu.micro.course.model.TipInfo;
import com.zhihuishu.micro.course.openapi.app.dto.TipInfoOpenDto;
import com.zhihuishu.micro.course.openapi.app.teacher.TipInfoOpenService;
import com.zhihuishu.micro.course.service.TipInfoService;
import com.zhihuishu.remote.RemoteResult;

@Service("tipInfoOpenServiceForTeacherApp")
public class TipInfoOpenServiceImpl implements TipInfoOpenService {

    @Resource
    private TipInfoService tipInfoService;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public RemoteResult<Void> saveOrUpdateTipInfo(TipInfoOpenDto tipInfoOpenDto) {
        RemoteResult<Void> remoteResult = new RemoteResult<Void>();

        try {
            Assert.notNull(tipInfoOpenDto, "对象不能为空");
            Assert.notNull(tipInfoOpenDto.getCourseId(), "课程id不能为null");
            Assert.notNull(tipInfoOpenDto.getUserId(), "用户id不能为空");
            Assert.notNull(tipInfoOpenDto.getModule(), "模块不能为空");
            Assert.isTrue(tipInfoOpenDto.getModule() > 0, "模块必须合法");
            Assert.isTrue(tipInfoOpenDto.getModule() < 7, "模块必须合法");
            Assert.notNull(tipInfoOpenDto.getChangePointEnum(), "修改点不能为空");
            int changePoint = tipInfoOpenDto.getChangePointEnum().getCode();// 修改点

            if (changePoint == 5 || changePoint == 6 || changePoint == 7 || changePoint == 8
                    || changePoint == 13) {// 这些修改点对应的id不能为null
                Assert.isTrue(tipInfoOpenDto.getPointId() > 0, "修改点的id不能为0");
            } else {
                Assert.isTrue(tipInfoOpenDto.getPointId() == 0, "修改点的id必须为0");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("接收到的参数信息为:{}", tipInfoOpenDto);
            }
            TipInfo tipInfo = new TipInfo();
            tipInfo.setChangePoint(tipInfoOpenDto.getChangePointEnum().getCode());// 修改点
            tipInfo.setCourseId(tipInfoOpenDto.getCourseId());
            tipInfo.setModule(tipInfoOpenDto.getModule());
            tipInfo.setPointId(tipInfoOpenDto.getPointId());
            tipInfo.setUserId(tipInfoOpenDto.getUserId());
            tipInfo.setLastModify(System.currentTimeMillis());
            tipInfo.setLastModifyDate(new Date());
            tipInfoService.saveOrUpdateTipInfo(tipInfo);// 保存或者更新修改记录

        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("保存或更新修改记录异常,参数为{}", tipInfoOpenDto, e);
        }
        return remoteResult;
    }

    @Override
    public RemoteResult<Void> deleteTipInfo(TipInfoOpenDto tipInfoOpenDto) {
        RemoteResult<Void> remoteResult = new RemoteResult<Void>();
        try {
            Assert.notNull(tipInfoOpenDto, "要删除的更新信息不能为空");
            Assert.notNull(tipInfoOpenDto.getPointId(), "删除点id不能为null");
            Assert.notNull(tipInfoOpenDto.getCourseId(), "课程id不能为null");
            Assert.notNull(tipInfoOpenDto.getModule(), "模块不能为空");
            Assert.notNull(tipInfoOpenDto.getChangePointEnum(), "修改点不能为空");
            TipInfo tipInfo = new TipInfo();
            tipInfo.setCourseId(tipInfoOpenDto.getCourseId());
            tipInfo.setModule(tipInfoOpenDto.getModule());
            tipInfo.setChangePoint(tipInfoOpenDto.getChangePointEnum().getCode());
            tipInfo.setPointId(tipInfoOpenDto.getPointId());
            tipInfoService.deleteTipInfo(tipInfo);
        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("删除tip信息出错,参数为{}", tipInfoOpenDto, e);
        }
        return remoteResult;
    }

    @Override
    public RemoteResult<Void> deleteTipInfoByLessonId(Integer lessonId, Long courseId) {
        RemoteResult<Void> remoteResult = new RemoteResult<Void>();
        try {
            Assert.notNull(lessonId, "参数不合法");
            Assert.notNull(courseId, "参数不合法");
            if (logger.isDebugEnabled()) {
                logger.debug("接收到小节的id为{},课程id为{}", lessonId, courseId);
            }
            tipInfoService.deleteTipInfoByLessonId(lessonId, courseId);
        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("删除节tip信息出错,参数为{}", lessonId, e);
        }
        return remoteResult;
    }

    @Override
    public RemoteResult<Void> deleteTipInfoByChapterId(Integer chapterId, Long courseId) {

        RemoteResult<Void> remoteResult = new RemoteResult<Void>();
        try {
            Assert.notNull(chapterId, "参数不合法");
            Assert.notNull(courseId, "参数不合法");
            if (logger.isDebugEnabled()) {
                logger.debug("接收到的章id:{} 课程id:{}", chapterId, courseId);
            }
            tipInfoService.deleteTipInfoByChapterId(chapterId, courseId);
        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("删除章tip信息出错,参数为{}", chapterId, e);
        }
        return remoteResult;

    }
    @Override
    public RemoteResult<Void> deleteTipInfoByLessonVideoId(int lessonVideoId,long courseId) {
        RemoteResult<Void> remoteResult=new RemoteResult<Void>();
        try {
            Assert.isTrue(lessonVideoId>0, "参数不合法");
            Assert.isTrue(courseId>0,"参数不合法");
            if (logger.isDebugEnabled()) {
                logger.debug("接收到小节id为{}",lessonVideoId);
            }
            tipInfoService.deletetTipInfoByLessonVideoId(lessonVideoId,courseId);
        } catch (Exception e) {
            remoteResult.setExceptionStack(e);
            remoteResult.setSuccess(false);
            logger.error("删除小节tip信息出错,参数为{}",lessonVideoId,e);
        }
        return remoteResult;

    }

}
